#include "COptionsState.h"
#include "CMainMenuState.h"
#include "CGame.h"
#include "CPrintBitmapToScreen.h"
#include "Wrappers\CSGD_DirectInput.h"
#include "Wrappers\CSGD_Direct3D.h"
#include "Wrappers\tinyxml.h"

COptionsState::COptionsState(void)
{
	m_nScreenWidth = CGame::GetInstance()->GetScreenWidth();
	m_nScreenHeight = CGame::GetInstance()->GetScreenHeight();


	
}

COptionsState::COptionsState(const COptionsState&)
{

}

COptionsState::~COptionsState()
{

}

COptionsState& COptionsState::operator=(const COptionsState&)
{
	return *this;
}

COptionsState* COptionsState::GetInstance(void)
{
	static COptionsState instance;
	return &instance;
}

bool COptionsState::Load(const char* szXmlFileName)
{
	LoadOptionsSettings(&m_fVolume, &m_fSFXVolume);
	return true;
}

bool COptionsState::Save(const char* szXmlFileName)
{
	SaveOptionsSettings(GetVolume(), GetSFXVolume());
	return true;
}

void COptionsState::SaveOptionsSettings(float fMusicVol, float fSfxVol)
{
	TiXmlDocument doc;

	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0", "utf-8", "");

	int nMusic = int(fMusicVol*100);
	int nSfx = int(fSfxVol * 100);

	
	doc.LinkEndChild(pDec);

	TiXmlElement* pRoot = new TiXmlElement("Options");
	TiXmlElement* pSetting = new TiXmlElement("Settings");

	pSetting->SetAttribute("MusicVolume", nMusic);
	pSetting->SetAttribute("SfxVolume", nSfx);

	pRoot->LinkEndChild(pSetting);
	doc.LinkEndChild(pRoot);

	doc.SaveFile("Resource/Options_Settings.xml");
}

bool COptionsState::LoadOptionsSettings(float* fMusicVol, float* fSfxVolume)
{
	TiXmlDocument doc;

	if(!doc.LoadFile("Resource/Options_Settings.xml")) // If it fails to load
		return false;

	int nMusic = -1;
	int nSfx = -1;

	TiXmlElement* pRoot = doc.RootElement();

	if(!pRoot)
		return false;

	pRoot = doc.FirstChildElement("Options");
	TiXmlElement* pSettings = pRoot->FirstChildElement("Settings");

	if(!pSettings)
		return false;

	pSettings->Attribute("MusicVolume", &nMusic);
	pSettings->Attribute("SfxVolume", &nSfx);

	*fMusicVol = float(nMusic*0.01f);
	*fSfxVolume = float(nSfx*0.01f);

	return true;
}

void COptionsState::Enter(void)
{
	// Set values
	m_nCurrentSelection		= 0;
	m_nCursorID				= -1;
	m_nBackgroundImageID	= -1;
	m_nBackgroundMusicID	= -1;
	m_nMovementSFXID		= -1;
	m_nSFXID				= -1;
	m_nBitmapFontID			= -1;


	m_nScreenWidth = CGame::GetInstance()->GetScreenWidth();
	m_nScreenHeight = CGame::GetInstance()->GetScreenHeight();
	//isFull = false;
	// Load sounds
	m_nMovementSFXID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_MenuSelection.wav");
	m_nBackgroundMusicID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_Course.mp3");

	// Load textures
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Cursor.png");
	m_nBackgroundImageID = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Graphics/Art Assets/cyberScape_logo.png");

	CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());
	CSGD_FModManager::GetInstance()->SetVolume(m_nBackgroundMusicID,COptionsState::GetInstance()->GetVolume());
	//
	Load("Options.xml");
}

bool COptionsState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(m_nCurrentSelection > 0)
			--m_nCurrentSelection;
		else
			m_nCurrentSelection = NUM_OPTIONS;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN)||CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(m_nCurrentSelection < NUM_OPTIONS)
			++m_nCurrentSelection;
		else
			m_nCurrentSelection = 0;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	// Adjust volume values
	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_LEFT))
	{
		if(m_nCurrentSelection == 1)
		{
			m_fVolume -= 0.001f;
			if(m_fVolume < 0.0f)
				m_fVolume = 0.0f;

			CSGD_FModManager::GetInstance()->SetVolume(m_nBackgroundMusicID, m_fVolume);

			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
			{
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nBackgroundMusicID);
			}
		}
		else if(m_nCurrentSelection == 2)
		{
			if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
				CSGD_FModManager::GetInstance()->StopSound(m_nBackgroundMusicID);
		
			m_fSFXVolume -= 0.001f;
			if(m_fSFXVolume < 0.0f)
				m_fSFXVolume = 0.0f;

			CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID, m_fSFXVolume);

			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nMovementSFXID))
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
		}
	}

	if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_RIGHT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirDown(DIR_RIGHT))
	{
		if(m_nCurrentSelection == 1)
		{
			m_fVolume += 0.001f;
			if(m_fVolume > 1.0f)
				m_fVolume = 1.0f;

			CSGD_FModManager::GetInstance()->SetVolume(m_nBackgroundMusicID, m_fVolume);

			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
			{
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nBackgroundMusicID);
			}
		}
		else if(m_nCurrentSelection == 2)
		{
			m_fSFXVolume += 0.001f;
			if(m_fSFXVolume > 1.0f)
				m_fSFXVolume = 1.0f;

			CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID, m_fSFXVolume);

			if(!CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nMovementSFXID))
				CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
		}
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{
		if(m_nCurrentSelection == 0)
			CGame::GetInstance()->PopState();

		if(m_nCurrentSelection == 3)
		{
			if(CGame::GetInstance()->GetFullscreen() == false)
			{
				CGame::GetInstance()->SetFullscreen(true);
				CSGD_Direct3D::GetInstance()->ChangeDisplayParam(m_nScreenWidth, m_nScreenHeight, CGame::GetInstance()->GetFullscreen());	
			}
			else
			{
				CGame::GetInstance()->SetFullscreen(false);
				CSGD_Direct3D::GetInstance()->ChangeDisplayParam(m_nScreenWidth, m_nScreenHeight, CGame::GetInstance()->GetFullscreen());				
			}
		}
	}

	return true;
}

void COptionsState::Update(float fElapsedTime)
{
	CSGD_FModManager::GetInstance()->SetVolume(m_nMovementSFXID,COptionsState::GetInstance()->GetSFXVolume());
}

void COptionsState::Render(void)
{
	// Render a background image
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImageID, 0, 0, 0.67f, 0.6f);

	// Display options
	CPrintBitmapFontToScreen::GetInstance()->Print("BACK", 170, 240, 0.5f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("SET MUSIC VOLUME", 170, 270, 0.5f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("SET SFX VOLUME", 170, 300, 0.5f, D3DCOLOR_XRGB(255,0,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("SET FULL SCREEN", 170, 330, 0.5f, D3DCOLOR_XRGB(255,0,0));

	char buffer[128] = {0};
	CSGD_Direct3D::GetInstance()->GetSprite()->Flush();


	sprintf_s(buffer, _countof(buffer), "<%.0f>", m_fVolume * 100);
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 500, 270, 255, 0, 0);

	sprintf_s(buffer, _countof(buffer), "<%.0f>", m_fSFXVolume * 100);
	CSGD_Direct3D::GetInstance()->DrawTextA(buffer, 500, 300, 255, 0, 0);

	if( CGame::GetInstance()->GetFullscreen() == false)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print("FULL SCREEN", 500, 330, 0.5f, D3DCOLOR_XRGB(255,0,0));
	}
	else
	{
		CPrintBitmapFontToScreen::GetInstance()->Print("WINDOWED",  500, 330, 0.5f, D3DCOLOR_XRGB(255,0,0));
	}
	// Temporary functionality display
	//CSGD_Direct3D::GetInstance()->DrawTextA("This is the options menu.", 200, 200, 255, 0, 0);

	// Draw cursor
	CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 130, 240 + m_nCurrentSelection * 30);
}

void COptionsState::Exit(void)
{
	// Set global values
	CGame::GetInstance()->SetMusicVolume(m_fVolume);
	CGame::GetInstance()->SetSFXVolume(m_fSFXVolume);

	// Stop the sounds
	if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
		CSGD_FModManager::GetInstance()->StopSound(m_nBackgroundMusicID);

	if(CSGD_FModManager::GetInstance()->IsSoundPlaying(m_nBackgroundMusicID))
		CSGD_FModManager::GetInstance()->StopSound(m_nBackgroundMusicID);

	// Unload assets
	//CSGD_FModManager::GetInstance()->UnloadSound(m_nMovementSFXID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nCursorID);

	// Save the options
	Save("Options.xml");
}